package jmars.mobile.worker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import jmars.mobile.common.Investigator;
import jmars.mobile.common.Sha;
import jmars.mobile.datainvestigation.Summary;

/**
 * 
 * group=1,2
 * @author Jan
 *
 */
public class BtsTrafficHourly implements Investigator {
	
	int[] workdays = {19,20,21,22};
	int[] weekends = {17,18};
	int[] processDays = {17,18,19,20,21,22};
	
	@Override
	public Long compute(File[] files, FileWriter writer) throws IOException, ParseException {
		
		Long total = 0L;
		FileInputStream fis;
		BufferedReader reader;
		String row;
		String[] cols;
		Map<Integer,Map<String,Map<Integer,Double>>> results=new HashMap<Integer,Map<String,Map<Integer,Double>>>();//Map<day,Map<BTS,Map<Hour,Traffic>>>
		Map<String,Map<Integer,Double>> workdayResults = new HashMap<String,Map<Integer,Double>>();
		Map<String,Map<Integer,Double>> weekendResults = new HashMap<String,Map<Integer,Double>>();
		for(int day:processDays){
			results.put(day, new HashMap<String,Map<Integer,Double>>());
		}
		Calendar cal=Calendar.getInstance();
		Date beginDate;
		String groupKey;
		Integer dayOfMonth;
		Integer hourOfDay;
		Set<Integer> tempWorkdays=new HashSet<Integer>();
		Set<Integer> tempWeekends=new HashSet<Integer>();
		int fileIndex=0;
		for(File inputFile:files){
			fileIndex++;
			fis = new FileInputStream(inputFile);
			reader = new BufferedReader(new InputStreamReader(fis));
			while((row=reader.readLine())!=null){
				cols=row.split(Summary.columnSeperator);
				if(Summary.filterRowExcludeLACI(cols,true,false)) continue;
				beginDate=Summary.parseBeginDate(cols);
				if(beginDate==null) continue;
				total++;
				cal.setTime(beginDate);
				if(cal.get(Calendar.YEAR)!=2012||(cal.get(Calendar.MONTH)!=10&&cal.get(Calendar.MONTH)!=11)) continue;
				dayOfMonth=cal.get(Calendar.DAY_OF_MONTH);
				hourOfDay=cal.get(Calendar.HOUR_OF_DAY);
				if(results.containsKey(dayOfMonth)){
					groupKey=Summary.genGroupKey(cols);
					if(!results.get(dayOfMonth).containsKey(groupKey))
						results.get(dayOfMonth).put(groupKey, new HashMap<Integer,Double>());
					if(!results.get(dayOfMonth).get(groupKey).containsKey(hourOfDay))
						results.get(dayOfMonth).get(groupKey).put(hourOfDay, Double.parseDouble(cols[10]));
					else
						results.get(dayOfMonth).get(groupKey).put(hourOfDay, results.get(dayOfMonth).get(groupKey).get(hourOfDay)+Double.parseDouble(cols[10]));
				}
			}
			reader.close();
			fis.close();
			System.out.println(fileIndex+" "+inputFile.getName()+" finished at "+total);
		}
		for(Integer d:workdays){
			if(results.containsKey(d)){
				tempWorkdays.add(d);
				for(String gkey:results.get(d).keySet()){
					if(!workdayResults.containsKey(gkey))
						workdayResults.put(gkey, new HashMap<Integer,Double>());
					for(Integer hhh:results.get(d).get(gkey).keySet()){
						if(!workdayResults.get(gkey).containsKey(hhh))
							workdayResults.get(gkey).put(hhh, results.get(d).get(gkey).get(hhh));
						else
							workdayResults.get(gkey).put(hhh, workdayResults.get(gkey).get(hhh)+results.get(d).get(gkey).get(hhh));
					}
				}
			}
		}
		for(Integer d:weekends){
			if(results.containsKey(d)){
				tempWeekends.add(d);
				for(String gkey:results.get(d).keySet()){
					if(!weekendResults.containsKey(gkey))
						weekendResults.put(gkey, new HashMap<Integer,Double>());
					for(Integer hhh:results.get(d).get(gkey).keySet()){
						if(!weekendResults.get(gkey).containsKey(hhh))
							weekendResults.get(gkey).put(hhh, results.get(d).get(gkey).get(hhh));
						else
							weekendResults.get(gkey).put(hhh, weekendResults.get(gkey).get(hhh)+results.get(d).get(gkey).get(hhh));
					}
				}
			}
		}
		writer.write("%total:"+Summary.intFormat.format(total)+"\r\n");
		writer.write("%bts,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23\r\n");
		writer.write("%*****************workdays******************\r\n");
		for(String gkey:workdayResults.keySet()){
			writer.write(Sha.digest32HexSHA1(gkey));
			for(Integer h=0;h<24;h++){
				if(workdayResults.get(gkey).containsKey(h))
					writer.write(","+Summary.intFormat.format(workdayResults.get(gkey).get(h)));
				else
					writer.write(",0");
			}
			writer.write("\r\n");
		}
		writer.write("%\r\n");
		writer.write("%*****************weekends******************\r\n");
		for(String gkey:weekendResults.keySet()){
			writer.write(Sha.digest32HexSHA1(gkey));
			for(Integer h=0;h<24;h++){
				if(weekendResults.get(gkey).containsKey(h))
					writer.write(","+Summary.intFormat.format(weekendResults.get(gkey).get(h)));
				else
					writer.write(",0");
			}
			writer.write("\r\n");
		}
		
		return total;
	}

}
